package com.chuangke.metadata.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.common.http.HttpResult;
import com.chuangke.metadata.entity.SysGridFormula;
import com.chuangke.metadata.entity.SysGridModel;
import com.chuangke.metadata.service.SysGridlFormulaService;

/**
 * @author chuangke
 * @date 2022-10-20
 */
@RestController
@RequestMapping("grid")
public class SysGridFormulaController {

	@Autowired
	private SysGridlFormulaService sysGridlFormulaService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/formula/save")
	public HttpResult save(@RequestBody SysGridModel record) {
		sysGridlFormulaService.save(record);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/formula/delete")
	public HttpResult delete(@RequestBody List<SysGridFormula> records) {
		sysGridlFormulaService.delete(records);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/formula/id/{id}")
	public HttpResult loadFormulaById(@PathVariable("id") String id) {
		return HttpResult.ok(sysGridlFormulaService.loadFormulaByModelId(id,true));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/formula/category/{category}")
	public HttpResult loadFormulaByCategory(@PathVariable("category") String category) {
		return HttpResult.ok(sysGridlFormulaService.loadFormulaByModelId(category,true));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/data/id/{id}")
	public HttpResult loadDataById(@PathVariable("id") String id, @RequestBody Map<String, Object> env) {
		return HttpResult.ok(sysGridlFormulaService.loadDataByModelId(id, env));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/data/category/{category}")
	public HttpResult loadDataByCategory(@PathVariable("category") String category,
			@RequestBody Map<String, Object> params) {
		Map<String, Object> env = new HashMap<>() ;
		env.put("T_ENTITY", params.get("T_ENTITY")) ;
		env.put("T_TIME", params.get("T_TIME")) ;
		return HttpResult.ok(sysGridlFormulaService.loadDataByCategory(category, env));
	}

}
